<?php
namespace Api\Controller;

/**
 * 数据字典
 */
use Common\Controller\AdminbaseController;

class DictionaryController extends AdminbaseController
{

    protected $model;

    function _initialize()
    {
        parent::_initialize();
        $this->model = M();
    }

    function index()
    {
        //查询所有表的SQL
        $no_show_field = array();   //不需要显示的字段
        $show_tables_sql = 'show tables';
        $tablePrefix = C('DB_PREFIX');
        $database = C('DB_NAME');
        $html = '';
        $nav = '';
        //查询所有的表
        $tables = array();
        $show_tables = $this->model->query($show_tables_sql);
        foreach ($show_tables as $show_table) {
            if (stripos($show_table['tables_in_'.$database], $tablePrefix) === 0) {
                $tables[]['TABLE_NAME'] = $show_table['tables_in_'.$database];
            }
        }
        //循环取得所有表的备注及表中列消息
        foreach ($tables as $k => $v) {
            $sql = 'SELECT * FROM ';
            $sql .= 'INFORMATION_SCHEMA.TABLES ';
            $sql .= 'WHERE ';
            $sql .= "table_name = '{$v['TABLE_NAME']}'  AND table_schema = '{$database}'";

            $table_infos = $this->model->query($sql);
            //查询到表的注释
            $tables[$k]['TABLE_COMMENT'] = $table_infos[0]['table_comment'];

            $sql = 'SELECT * FROM ';
            $sql .= 'INFORMATION_SCHEMA.COLUMNS ';
            $sql .= 'WHERE ';
            $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";

            $table_columns = $this->model->query($sql);

            $tables[$k]['COLUMN'] = $table_columns;

        }

        //循环所有表
        foreach ($tables as $k => $v) {
            $nav.='<a href="#'.$v['TABLE_NAME'].'">' .($k + 1) . '、' . $v['TABLE_COMMENT'] . ' </a> <br>';
            $html .= '	<h3 id="'.$v['TABLE_NAME'].'">' . ($k + 1) . '、' . $v['TABLE_COMMENT'] . '  （' . $v['TABLE_NAME'] . '）</h3>' . "\n";
            $html .= '	<table border="1" cellspacing="0" cellpadding="0" width="100%">' . "\n";
            $html .= '		<tbody>' . "\n";
            $html .= '			<tr>' . "\n";
            $html .= '				<th>字段名</th>' . "\n";
            $html .= '				<th>数据类型</th>' . "\n";
            $html .= '				<th>默认值</th>' . "\n";
            $html .= '				<th>允许非空</th>' . "\n";
            $html .= '				<th>自动递增</th>' . "\n";
            $html .= '				<th>备注</th>' . "\n";
            $html .= '			</tr>' . "\n";

            foreach ($v['COLUMN'] as $f) {
                if (!is_array($no_show_field[$v['TABLE_NAME']])) {
                    $no_show_field[$v['TABLE_NAME']] = array();
                }
                if (!in_array($f['column_name'], $no_show_field[$v['TABLE_NAME']])) {
                    $html .= '			<tr>' . "\n";
                    $html .= '				<td class="c1">' . $f['column_name'] . '</td>' . "\n";
                    $html .= '				<td class="c2">' . $f['column_type'] . '</td>' . "\n";
                    $html .= '				<td class="c3">' . $f['column_default'] . '</td>' . "\n";
                    $html .= '				<td class="c4">' . $f['is_nullable'] . '</td>' . "\n";
                    $html .= '				<td class="c5">' . ($f['extra'] == 'auto_increment' ? '是' : '&nbsp;') . '</td>' . "\n";
                    $html .= '				<td class="c6">' . $f['column_comment'] . '</td>' . "\n";
                    $html .= '			</tr>' . "\n";
                }
            }
            $html .= '		</tbody>' . "\n";
            $html .= '	</table>' . "\n";
        }

        $this->assign("html", $html);
        $this->assign("nav", $nav);
        $this->display("index");
    }

}